-- ****************************************************************************************************
-- SELECT INCREMENTAL UPDATE Stored Procedures for the list of tables mentioned in the Initialize file
--------------------------------------------------------------------------------------------------

--TABLE NAME: CustomerContact    STORED PROCEDURE NAME: Sales.sync_sp_CustomerContact_SelectIncrementalUpdates

use [SyncSamplesDb_ChangeTracking_Custom]

if object_id(N'Sales.sync_sp_CustomerContact_SelectIncrementalUpdates', 'P') is not null
	drop procedure Sales.sync_sp_CustomerContact_SelectIncrementalUpdates
GO	

		create procedure Sales.sync_sp_CustomerContact_SelectIncrementalUpdates (	
			@sync_last_received_anchor bigint, 
			@sync_new_received_anchor bigint, 
			@sync_initialized bit,
			@sync_client_id_binary varbinary)   
AS
	IF @sync_initialized > 0  
	BEGIN 
			SELECT		E.CustomerId
						, E.PhoneNumber
						, E.PhoneType
			FROM		[SyncSamplesDb_ChangeTracking_Custom].[Sales].[CustomerContact] AS E
			JOIN		CHANGETABLE(CHANGES [SyncSamplesDb_ChangeTracking_Custom].[Sales].[CustomerContact], @sync_last_received_anchor) AS CT
						ON E.CustomerId = CT.CustomerId
						AND E.PhoneType = CT.PhoneType
			WHERE		CT.SYS_CHANGE_VERSION <= @sync_new_received_anchor
						AND CT.SYS_CHANGE_OPERATION = 'U'
						AND (CT.SYS_CHANGE_CONTEXT IS NULL 
							OR CT.SYS_CHANGE_CONTEXT <> @sync_client_id_binary) 


		IF (CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('[SyncSamplesDb_ChangeTracking_Custom].[Sales].[CustomerContact]')) > @sync_last_received_anchor)
			RAISERROR (N'SQL Server Change Tracking has cleaned up tracking information for table ''%s''. 
						To recover from this error, the client must reinitialize its local database and try again'
						,16,3,'[SyncSamplesDb_ChangeTracking_Custom].[Sales].[CustomerContact]')  
	END

GO
-------------------------------------------END------------------------------------------------------------------
